﻿using System;
using System.Linq;

namespace TV_Organizer.EPGuides
{
    class EPGuidesShow : TVRage.TVRageShow
    {
        private EPGuidesShow(string tvRageId, string name)
            : base(tvRageId, name)
        {
        }

        public static EPGuidesShow CreateFromSerializedValue(string serializedValue)
        {
            var l_delimiterIndexes = serializedValue.IndexesOf(Constants.AllShowsTXTDelimiter);

            if (l_delimiterIndexes.Length % 2 != 0)
                throw new InvalidOperationException();

            var l_separatorIndexes = serializedValue.IndexesOf(Constants.AllShowsTXTSeparator);

            for (int i = 0; i < l_delimiterIndexes.Length; i = i + 2)
            {
                var l_separatorIndexesCopy = l_separatorIndexes.ToList();
                for (int ii = 0; ii < l_separatorIndexes.Length; ii++)
                {
                    if (l_separatorIndexes[ii] > l_delimiterIndexes[i] && l_separatorIndexes[ii] < l_delimiterIndexes[i + 1])
                        l_separatorIndexesCopy.RemoveAt(ii);
                }
                l_separatorIndexes = l_separatorIndexesCopy.ToArray();
            }

            if (l_separatorIndexes.Length != 8)
                throw new InvalidOperationException();

            var l_trimChars = new char[] { Constants.AllShowsTXTDelimiter, Constants.AllShowsTXTSeparator };

            var name = serializedValue.Substring(0, l_separatorIndexes[0]).Trim(l_trimChars);
            var tvRageId = serializedValue.Substring(l_separatorIndexes[1] + 1, l_separatorIndexes[2] - l_separatorIndexes[1]).Trim(l_trimChars);

            var result = new EPGuidesShow(tvRageId, name);

            result.SerializedValue = serializedValue;
            result.Directory = serializedValue.Substring(l_separatorIndexes[0] + 1, l_separatorIndexes[1] - l_separatorIndexes[0]).Trim(l_trimChars);
            result.StartDate = serializedValue.Substring(l_separatorIndexes[2] + 1, l_separatorIndexes[3] - l_separatorIndexes[2]).Trim(l_trimChars);
            result.EndDate = serializedValue.Substring(l_separatorIndexes[3] + 1, l_separatorIndexes[4] - l_separatorIndexes[3]).Trim(l_trimChars);
            result.EpisodesNumber = serializedValue.Substring(l_separatorIndexes[4] + 1, l_separatorIndexes[5] - l_separatorIndexes[4]).Trim(l_trimChars);
            result.Runtime = serializedValue.Substring(l_separatorIndexes[5] + 1, l_separatorIndexes[6] - l_separatorIndexes[5]).Trim(l_trimChars);
            result.Network = serializedValue.Substring(l_separatorIndexes[6] + 1, l_separatorIndexes[7] - l_separatorIndexes[6]).Trim(l_trimChars);
            result.Country = serializedValue.Substring(l_separatorIndexes[7] + 1, serializedValue.Length - l_separatorIndexes[7] - 1).Trim(l_trimChars);

            return result;
        }

        public string SerializedValue { get; private set; }

        public string Directory { get; private set; }
        public string StartDate { get; private set; }
        public string EndDate { get; private set; }
        public string EpisodesNumber { get; private set; }
        public string Runtime { get; private set; }
        public string Network { get; private set; }
        public string Country { get; private set; }
    }
}
